package org.spolo.android.support.enventmanager;

import android.annotation.SuppressLint;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 此类实现了EventQueueInterface,提供了i个简单的注册,派发,删除
 * 
 * @date 2012-7-23
 */
public class EventQueue implements EventQueueInterface {
	@SuppressLint("UseSparseArrays")
	private Map<Integer, List<EventHandlerInterface>> eventMap = new HashMap<Integer, List<EventHandlerInterface>>();

	@Override
	public void addListener(int key, EventHandlerInterface handler) {
		List<EventHandlerInterface> l = null;
		if (this.eventMap.containsKey(key)) {
			l = this.eventMap.get(key);
		} else {
			l = new ArrayList<EventHandlerInterface>();
			this.eventMap.put(key, l);
		}
		l.add(handler);
	}

	@Override
	public void removeListner(int key) {
		this.eventMap.remove(key);
	}

	@Override
	public void publish(int key, Object parm) {
		List<EventHandlerInterface> l = this.eventMap.get(key);
		if (l != null) {
			for (EventHandlerInterface i : l) {
				i.execute(parm);
			}
		}

	}

	@Override
	public Map<Integer, List<EventHandlerInterface>> getEventMap() {
		return eventMap;
	}
}
